home *** CD-ROM | disk | FTP | other *** search
/ Network Supervisor's Toolkit / Network Supervisor's Toolkit.iso / btrieve / btp15 / example1.pas < prev    next >
Pascal/Delphi Source File  |  1996-07-10  |  7KB  |  253 lines

  1. PROGRAM Example1;            { (C) 1991 John C. Leon   last updated 11/5/91 }
  2.  
  3. {Use Btrieve data file EXAMPLE created with CREATE.PAS}
  4.  
  5. {$IFDEF production} {$D-,R-,L-,S-} {$ENDIF}
  6.  
  7. USES
  8.     Crt, BTP;
  9.  
  10. TYPE
  11.    ExampleFields  = record
  12.                     case integer of
  13.                     1: (First   : array[1..10] of char; {define 3 fields     }
  14.                         Last    : array[1..20] of char;
  15.                         KeyBuf  : array[1..20] of char);{size to largest key }
  16.                     2: (DBuffer : array[1..30] of char);{size to rec length  }
  17.                     3: (Position: array[1..2] of word); {high word first!    }
  18.                     end;                                {useful after GETPOS }
  19.    PExample      = ^ExampleFile;
  20.    ExampleFile   = object(BFile)
  21.                       Fields : ExampleFields;
  22.                       constructor Init;
  23.                       function BT(OpCode, Key:integer):integer; virtual;
  24.                       procedure SetFields;
  25.                       procedure GetFields;
  26.                       function GetNumRecs:longint;
  27.                       function GetName: string;
  28.                       procedure SetKeyZero;
  29.                       procedure SetKeyOne;
  30.                       end;
  31.  
  32. VAR
  33.    Example       : PExample;
  34.    FName         : string[10];
  35.    LName         : string[20];
  36.    Name          : string;
  37.    NumberRecords,
  38.    Position      : longint;
  39.  
  40. (* Define methods of ExampleFile *)
  41. (* ------------------------------------------------------------------------ *)
  42.  
  43. constructor ExampleFile.Init;
  44. begin
  45.    BFile.Init('example', Accel);              {Open file in accelerated mode.}
  46. end;
  47.  
  48. function ExampleFile.BT(OpCode, Key:integer):integer;
  49. begin
  50.    DBufferLen := Specs.RecLen;
  51.    BT := Btrv(OpCode, PosBlk, Fields, DBufferLen, Fields.KeyBuf, Key);
  52. end;
  53.  
  54. procedure ExampleFile.SetFields;
  55. var Counter:integer;
  56. begin
  57. with Fields do
  58.    begin
  59.    for Counter := 1 to length(FName) do
  60.       First[Counter] := FName[Counter];
  61.    if length(FName) < 10 then
  62.       for Counter := length(FName)+1 to 10 do
  63.       First[Counter] := ' ';
  64.    for Counter := 1 to length(LName) do
  65.       Last[Counter] := LName[Counter];
  66.    if length(LName) < 20 then
  67.       for Counter := length(LName)+1 to 20 do
  68.       Last[Counter] := ' ';
  69.    end;
  70. end;
  71.  
  72. procedure ExampleFile.GetFields;
  73. begin
  74.    with Fields do
  75.       begin
  76.       FName   := First;
  77.       LName   := Last;
  78.       end;
  79. end;
  80.  
  81. function ExampleFile.GetNumRecs: longint;
  82. begin
  83.    GetNumRecs := NumRecs;
  84. end;
  85.  
  86. function ExampleFile.GetName: string;
  87. var
  88.    First, Last : string;
  89. begin
  90.    First := RTrim(Fields.First);
  91.    Last  := RTrim(Fields.Last);
  92.    GetName := First + ' ' + Last;
  93. end;
  94.  
  95. procedure ExampleFile.SetKeyZero;
  96. var
  97.    Counter: integer;
  98. begin
  99.    with Fields do
  100.       begin
  101.       for Counter := 1 to length(LName) do
  102.          KeyBuf[Counter] := LName[Counter];
  103.       if length(LName) < 20 then
  104.          for Counter := length(LName) + 1 to 20 do
  105.             KeyBuf[Counter] := ' ';
  106.       end;
  107. end;
  108.  
  109. procedure ExampleFile.SetKeyOne;
  110. var
  111.    Counter: integer;
  112. begin
  113.    with Fields do
  114.       begin
  115.       for Counter := 1 to length(FName) do
  116.          KeyBuf[Counter] := FName[Counter];
  117.       if length(FName) < 10 then
  118.          for Counter := length(FName) + 1 to 10 do
  119.             KeyBuf[Counter] := ' ';
  120.       end;
  121. end;
  122.  
  123. (* End of method definitions for ExampleFile *)
  124. (* ------------------------------------------------------------------------ *)
  125.  
  126.  
  127. procedure ShowResults;
  128. begin
  129.    Name := Example^.GetName;
  130.    writeln(Name);
  131.    writeln('Status = ', BStatus, '.  Hit enter.');
  132.    readln;
  133. end;
  134.  
  135. procedure DoAnInsert;
  136. begin
  137.    write('Enter First Name (max 10): ');
  138.    readln(FName);
  139.    write('Enter Last Name (max 20): ');
  140.    readln(LName);
  141.    Example^.SetFields;
  142.    BStatus := Example^.BT(BInsert, Zero);
  143.    if BStatus = Zero then
  144.       begin
  145.       writeln('Record inserted OK.  Hit enter to continue.');
  146.       readln;
  147.       end
  148.       else
  149.       begin
  150.          writeln;
  151.          writeln;
  152.          writeln('Error in insert op...Status = ', BStatus);
  153.          writeln('Hit <Enter> to continue.');  {example program will now go }
  154.          readln;                               {on to DoTestOtherOps        }
  155.          clrscr;
  156.       end;
  157. end; {procedure DoAnInsert}
  158.  
  159. procedure DoTestOtherOps;
  160. var
  161.    Counter: integer;
  162. begin
  163.    BStatus := Example^.Close;                   {Close after insert for grins}
  164.    Example^.Init;        {Reinitialize the entire object from scratch.  Easy!}
  165.    clrscr;
  166.  
  167.    {NOW DOING A GET FIRST}
  168.    writeln('Doing get first on key zero.');
  169.    BStatus := Example^.BT(BGetFirst, Zero);
  170.    ShowResults;
  171.  
  172.    {NOW DO 5 GET NEXT OPS}
  173.    writeln('Now getting 5 next.');
  174.    for Counter := 1 to 5 do
  175.       begin
  176.       BStatus := Example^.BT(BGetNext, Zero);
  177.       Name := Example^.GetName;
  178.       writeln(Name);
  179.       end;
  180.    writeln('Status of fifth Get Next is ', BStatus, '.  Hit enter.');
  181.    readln;
  182.  
  183.    {NOW DO GET PREV OP}
  184.    writeln('Now getting previous:');
  185.    BStatus := Example^.BT(BGetPrev, Zero);
  186.    ShowResults;
  187.  
  188.    {NOW DO GET EQUAL OP ON 'LEON'}
  189.    LName := 'Leon';
  190.    Example^.SetKeyZero;               {sets keybuf to 'Leon' padded by blanks}
  191.    writeln('Now doing get equal on "', Example^.Fields.KeyBuf, '".');
  192.    BStatus := Example^.BT(BGetEqual, Zero);
  193.    ShowResults;
  194.  
  195.    {NOW DO GET EQUAL OP ON 'JOHN' IN KEY ONE}
  196.    FName := 'John';
  197.    Example^.SetKeyOne;
  198.    writeln('Now doing get equal on key 1..."', Example^.Fields.KeyBuf, '".');
  199.    BStatus := Example^.BT(BGetEqual, 1);
  200.    ShowResults;
  201.  
  202.    {NOW DO GET LESS THAN OP}
  203.    LName := 'Leon';
  204.    Example^.SetKeyZero;
  205.    writeln('Now doing less than on "', Example^.Fields.KeyBuf, '".');
  206.    BStatus := Example^.BT(BGetLess, Zero);
  207.    ShowResults;
  208.  
  209.    {NOW DO GET LESS THAN OR EQUAL OP}
  210.    LName := 'Leon';
  211.    Example^.SetKeyZero;
  212.    writeln('Now doing less than or equal to on "', Example^.Fields.KeyBuf, '".');
  213.    BStatus := Example^.BT(BGetLessEq, Zero);
  214.    ShowResults;
  215.  
  216.    {NOW DO GET LAST OP}
  217.    writeln('Now getting last in index path: ');
  218.    BStatus := Example^.BT(BGetLast, Zero);
  219.    ShowResults;
  220.  
  221.    {SHOW PHYSICAL POSITION OF LAST RECORD}
  222.    BStatus := Example^.BT(BGetPos, Zero);
  223.    Position := Example^.Fields.Position[2] + Example^.Fields.Position[1]*65536;
  224.    writeln;
  225.    writeln('Position (offset into file) of last record in path is ', Position);
  226.  
  227.    BStatus := Example^.Close;                  {close and reInit to get stats}
  228.    Example^.Init;
  229.    NumberRecords := Example^.GetNumRecs;
  230.    writeln('Total Number of Records is: ', NumberRecords, '.  Hit enter.');
  231.    readln;
  232. end;
  233.  
  234.  
  235. (* begin main program code *)
  236. (* ------------------------------------------------------------------------ *)
  237. VAR
  238.    Response: string;
  239. BEGIN
  240.    Example := new(PExample, Init);
  241.    repeat
  242.    clrscr;
  243.    DoAnInsert;
  244.    DoTestOtherOps;
  245.    write('Continue? (y/n): ');
  246.    readln(Response);
  247.    until (Response='N') or (Response='n');
  248.    BStatus := Example^.Close;
  249.    if BStatus <> 0 then
  250.       writeln('Problem closing file.  Status = ', BStatus, '.');
  251.    dispose(Example, Done);
  252. END.
  253.